import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yi_shop/model/address.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/tools.dart';

class AddressAddController extends GetxController {
  AddressReceiveModel model = AddressReceiveModel(name: '', mobile: '', district: '', address: '');

  late TextEditingController textEditingController;

  RxBool isDefault = false.obs;

  @override
  void onInit() {
    textEditingController = ToolsUtils.editingController('');
    super.onInit();
  }

 @override
  void onClose() {
    textEditingController.dispose();
    super.onClose();
  }
}

class AddressAddPage extends GetView<AddressAddController> {
  const AddressAddPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorsUtils.backgroundColor,
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '添加地址',
          style: StylesUtils.customTextStyle(
              fontSize: StylesUtils.fontSize18, fontWeight: FontWeight.w700),
        ),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 25.h),
        child: Column(
          children: [
            Row(
              children: [
                Container(
                  width: 80,
                  alignment: Alignment.center,
                  child: Text(
                    '收件人：',
                    style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color6666,
                    ),
                  ),
                ),
                Expanded(
                    child: Container(
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc)))),
                  child: TextField(
                    controller: ToolsUtils.editingController(controller.model.name),
                    cursorHeight: 15,
                    cursorWidth: 3,
                    cursorColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                    cursorRadius: const Radius.circular(2),
                    cursorOpacityAnimates: false,
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    decoration: InputDecoration(
                        hintText: '请输入收件人名称',
                        hintStyle: StylesUtils.customTextStyle(
                            color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
                        border: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        isCollapsed: false,
                        isDense: false,
                        contentPadding: const EdgeInsets.only(left: 15, bottom: 10),
                        constraints: const BoxConstraints.expand(height: 30)),
                    onChanged: (String value) {
                      controller.model.name = value;
                    },
                  ),
                ))
              ],
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: Row(
                children: [
                  Container(
                    width: 80,
                    alignment: Alignment.center,
                    child: Text(
                      '手机号码：',
                      style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color6666,
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                                color:
                                    ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc)))),
                    child: TextField(
                      controller: ToolsUtils.editingController(controller.model.mobile),
                      keyboardType: TextInputType.phone,
                      cursorHeight: 15,
                      cursorWidth: 3,
                      cursorColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                      cursorRadius: const Radius.circular(2),
                      cursorOpacityAnimates: false,
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                      decoration: InputDecoration(
                          hintText: '请输入手机号码',
                          hintStyle: StylesUtils.customTextStyle(
                              color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          isCollapsed: false,
                          isDense: false,
                          contentPadding: const EdgeInsets.only(left: 15, bottom: 10),
                          constraints: const BoxConstraints.expand(height: 30)),
                      onChanged: (String value) {
                        controller.model.mobile = value;
                      },
                    ),
                  ))
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: Row(
                children: [
                  Container(
                    width: 80,
                    alignment: Alignment.center,
                    child: Text(
                      '所在地区：',
                      style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color6666,
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                                color:
                                    ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc)))),
                    child: TextField(
                      controller: controller.textEditingController,
                      keyboardType: TextInputType.phone,
                      cursorHeight: 15,
                      cursorWidth: 3,
                      cursorColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                      cursorRadius: const Radius.circular(2),
                      cursorOpacityAnimates: false,
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                      decoration: InputDecoration(
                          hintText: '',
                          hintStyle: StylesUtils.customTextStyle(
                              color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          isCollapsed: false,
                          isDense: false,
                          contentPadding: const EdgeInsets.only(left: 15, bottom: 10),
                          constraints: const BoxConstraints.expand(height: 30),
                          suffixIcon: Icon(
                            Icons.arrow_forward_ios,
                            size: 15,
                            color: ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                          )),
                      readOnly: true,
                      onTap: () async {
                        Result? result = await CityPickers.showCityPicker(
                            context: context, height: 300, itemExtent: 40);
                        if (result != null) {
                          controller.textEditingController.text =
                              '${result.provinceName}${result.cityName}${result.areaName}';
                          controller.model.district = controller.textEditingController.text;
                        }
                      },
                    ),
                  ))
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: Row(
                children: [
                  Container(
                    width: 80,
                    alignment: Alignment.center,
                    child: Text(
                      '详细地址：',
                      style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color6666,
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                                color:
                                    ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc)))),
                    child: TextField(
                      controller: ToolsUtils.editingController(controller.model.address),
                      keyboardType: TextInputType.text,
                      cursorHeight: 15,
                      cursorWidth: 3,
                      cursorColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                      cursorRadius: const Radius.circular(2),
                      cursorOpacityAnimates: false,
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                      decoration: InputDecoration(
                          hintText: '请输入详细地址',
                          hintStyle: StylesUtils.customTextStyle(
                              color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          isCollapsed: false,
                          isDense: false,
                          contentPadding: const EdgeInsets.only(left: 15, bottom: 10),
                          constraints: const BoxConstraints.expand(height: 30)),
                      onChanged: (String value) {
                        controller.model.address = value;
                      },
                    ),
                  ))
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              alignment: Alignment.centerLeft,
              child: Wrap(
                spacing: 5,
                alignment: WrapAlignment.start,
                crossAxisAlignment: WrapCrossAlignment.center,
                children: [
                  InkWell(
                      onTap: () {
                        controller.isDefault.value = !controller.isDefault.value;
                        controller.model.checked = controller.isDefault.value;
                      },
                      child: Obx(() => SizedBox(
                            width: 20,
                            height: 20,
                            child: CustomPaint(
                              painter: _MyCustomCircular(checked: controller.isDefault.value),
                            ),
                          ))),
                  Text(
                    '设为默认地址',
                    style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color6666,
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              alignment: Alignment.center,
              child: TextButton(
                  onPressed: () {},
                  style: ButtonStyle(
                      alignment: Alignment.center,
                      fixedSize: const MaterialStatePropertyAll(Size(330, 40)),
                      minimumSize: const MaterialStatePropertyAll(Size(330, 40)),
                      backgroundColor: MaterialStatePropertyAll(
                          ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048)),
                      shape: MaterialStatePropertyAll(
                          RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)))),
                  child: Text(
                    '保存',
                    style: StylesUtils.customTextStyle(),
                  )),
            )
          ],
        ),
      ),
    );
  }
}

class _MyCustomCircular extends CustomPainter {
  final bool checked;

  _MyCustomCircular({this.checked = false});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = ColorsUtils.parseColorFromHexString(ColorsUtils.color9999)
      ..strokeWidth = 1
      ..style = PaintingStyle.stroke
      ..isAntiAlias = true;
    if (checked) {
      paint.style = PaintingStyle.fill;
      paint.color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorF95e);
    }
    canvas.drawCircle(Offset(size.width / 2, size.height / 2), size.width / 2, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
